﻿using Avalonia.BiliBili.Libs;
using Avalonia.BiliBili.Libs.Extensions;
using Bilibili.Common.Database;
using Bilibili.Common.Log;
using Bilibili.Model.Common;
using Bilibili.WebClient.Common;
using ReactiveUI;
using System;
using System.Reactive;

namespace Avalonia.BiliBili.Views.ViewModels
{
    public sealed class SettingViewModel : ViewModelBase
    {
        private readonly SettingView owner;
        public ReactiveCommand<string, Unit> OnButtonClick { get; }
        public SettingViewModel(SettingView settingView)
        {
            owner = settingView;
            OnButtonClick = ReactiveCommand.Create<string>(ButtonClick);
        }
        private void ButtonClick(string obj)
        {
            try
            {
                switch (obj)
                {
                    case "logout":
                        SettingHelper.UserID = 0;
                        SettingHelper.UserInfo = null;
                        SettingHelper.UserHead = string.Empty;
                        SettingHelper.AccessKey = string.Empty;
                        SettingHelper.BiliPlusCookie = string.Empty;
                        SettingHelper.UserInfoCookie = string.Empty;
                        ApiProvider.Instance.Clear();
                        StaticValue.CurrentUserIcon = new Uri("avares://Avalonia.BiliBili/Assets/Login/noavatar.png").ToBitmap();
                        StaticValue.CurrentUserId = 0;
                        StaticValue.CurrentUserName = "请登录";
                        owner.Callback(new InvokeData<object>("logout"));
                        break;
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance.LogError("ButtonClick", ex);
            }
        }
    }
}